
#include "voice_common.h"
#include "gkt_vpwm.h"

#ifdef GKT_CONFIG_VOICE_OUTPUT_VPWM_ID
#define VOICE_OUTPUT_VPWM_ID	GKT_CONFIG_VOICE_OUTPUT_VPWM_ID
#else
#define VOICE_OUTPUT_VPWM_ID	0
#endif

static void voice_output_vpwm_callback(uint32_t vpwm_id, 
					gkt_vpwm_status_e status)
{
	voice_output_status_e status;

	if (GKT_VPWM_STATUS_BLOCK_END == status)
		status = VOICE_OUTPUT_ST_BLOCK_END;
	else if (GKT_VPWM_STATUS_END == status)
		status = VOICE_OUTPUT_ST_END;
	else
		status = VOICE_OUTPUT_ST_ERROR;
	woice_output_status_notify(status);
}

int voice_output_hw_init(void)
{
	gkt_vpwm_cfg_attr_s vpwm_cfg_attr;
	int retval;

	vpwm_cfg_attr.frame_sample = GKT_VOICE_DEC_FRAME_SAMPLE;
	vpwm_cfg_attr.src_width = GKT_VOICE_DEC_SAMPLE_POINT_BITS;
	vpwm_cfg_attr.pcm_gain = GKT_VPWM_PCM_GAIN_1X;
	vpwm_cfg_attr.flags = GKT_VPWM_F_DATA_SIGNED;
	vpwm_cfg_attr.callback = voice_output_vpwm_callback;
	vpwm_cfg_attr.block_nums = VOICE_OUTPUT_DEC_BLOCK_NUMS;
	vpwm_cfg_attr.block_size = VOICE_OUTPUT_DEC_BLOCK_BYTES;
	retval = gkt_vpwm_config(VOICE_OUTPUT_VPWM_ID, &vpwm_cfg_attr);
	if (retval != GKT_SUCCESS)
		gkt_error("voice_output: config vpwm failed : %d\n", retval);

	return retval;
}

int voice_output_hw_start(const uint8_t *data, uint32_t size)
{
	int retval;

	retval = gkt_vpwm_start(VOICE_OUTPUT_VPWM_ID, (uint32_t)data, size);
	if (retval != GKT_SUCCESS) {
		gkt_error("voice_output: [PLAY] start vpwm failed %d\n", retval);
	}

	return retval;
}

void voice_output_hw_stop(void)
{
	gkt_vpwm_stop(VOICE_OUTPUT_VPWM_ID);
}

